﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.Hotel.UI.Controls;

namespace WHC.Hotel.UI
{
    public partial class FrmRoomStausAnalysis :BaseForm
    {
        public FrmRoomStausAnalysis()
        {
            InitializeComponent();

            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);
            this.Width = rect.Width - 40;
            dgView.Width = rect.Width - 58;
            dgView.ContextMenuStrip = this.contextMenuStrip1;
        }
        private void FrmRoomStausAnalysis_Load(object sender, EventArgs e)
        {
            CreateReport();
        }

        private void CreateReport()
        {

            //先生成表头


            dgView.Columns.Clear();

            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.HeaderText = "营业日";
            column.DisplayIndex = 1;
            column.Width =120;
            column.DataPropertyName = "currentday";
            column.Name = "currentday";
            column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgView.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.HeaderText = "总房数";
            column.DisplayIndex =2;
            column.Width = 60;
            column.DataPropertyName = "roomsum";
            column.Name = "roomsum";
            column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgView.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.HeaderText = "可租数";
            column.DisplayIndex = 3;
            column.Width = 60;
            column.DataPropertyName = "canusesum";
            column.Name = "canusesum";
            column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgView.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.HeaderText = "已租数";
            column.DisplayIndex = 4;
            column.Width = 60;
            column.DataPropertyName = "inusesum";
            column.Name = "inusesum";
            column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgView.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.HeaderText = "离店数";
            column.DisplayIndex = 5;
            column.Width = 60;
            column.DataPropertyName = "departuresum";
            column.Name = "departuresum";
            column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgView.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.HeaderText = "出租率";
            column.DisplayIndex = 6;
            column.Width = 60;
            column.DataPropertyName = "inuseratesum";
            column.Name = "inuseratesum";
            column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgView.Columns.Add(column);

            this.dgView.AddSpanHeader(1, 5, "总计");

            DataTable roomdt = new DataTable();
            roomdt.Columns.Add("currentday", typeof(string));
            roomdt.Columns.Add("roomsum", typeof(int));
            roomdt.Columns.Add("canusesum", typeof(int));
            roomdt.Columns.Add("inusesum", typeof(int));
            roomdt.Columns.Add("departuresum", typeof(int));
            roomdt.Columns.Add("inuseratesum", typeof(string));

             DataTable dt = BLLFactory<WHC.Hotel.BLL.RoomType>.Instance.GetAllToDataTable();
             int iCount = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {


                    column = new DataGridViewTextBoxColumn();
                    column.HeaderText = "总房数";
                    column.DisplayIndex =7+i*4;
                    column.Width =60;
                    column.DataPropertyName = dt.Rows[i]["Note"].ToString()+ "roomsum";
                    column.Name = dt.Rows[i]["Note"].ToString() + "roomsum";
                    column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgView.Columns.Add(column);
                    roomdt.Columns.Add(dt.Rows[i]["Note"].ToString() + "roomsum", typeof(int));

                    column = new DataGridViewTextBoxColumn();
                    column.HeaderText = "可租数";
                    column.DisplayIndex = 8 + i * 4;
                    column.Width = 60;
                    column.DataPropertyName =dt.Rows[i]["Note"].ToString()+ "canusesum";
                    column.Name =dt.Rows[i]["Note"].ToString() +"canusesum";
                    column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgView.Columns.Add(column);
                    roomdt.Columns.Add(dt.Rows[i]["Note"].ToString() + "canusesum", typeof(int));

                    column = new DataGridViewTextBoxColumn();
                    column.HeaderText = "已租数";
                    column.DisplayIndex = 9 + i * 4;
                    column.Width = 60;
                    column.DataPropertyName =dt.Rows[i]["Note"].ToString() +"inusesum";
                    column.Name = dt.Rows[i]["Note"].ToString() + "inusesum";
                    column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgView.Columns.Add(column);
                    roomdt.Columns.Add(dt.Rows[i]["Note"].ToString() + "inusesum", typeof(int));

                    column = new DataGridViewTextBoxColumn();
                    column.HeaderText = "离店数";
                    column.DisplayIndex = 10 + i * 4;
                    column.Width = 60;
                    column.DataPropertyName = dt.Rows[i]["Note"].ToString() + "departuresum";
                    column.Name = dt.Rows[i]["Note"].ToString() + "departuresum";
                    column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgView.Columns.Add(column);
                    roomdt.Columns.Add(dt.Rows[i]["Note"].ToString() + "departuresum", typeof(int));


                    this.dgView.AddSpanHeader(6 + i * 4, 4, dt.Rows[i]["Name"].ToString() + "(" + dt.Rows[i]["Note"].ToString() + ")");
                
                    
                
                
                }


           
                //this.dgView.MergeColumnNames.Add("Column1");
              
               //生成数据

                List<RoomInfo> roomlist = BLLFactory<WHC.Hotel.BLL.Room>.Instance.FindAllRooms("  1=1 ");
                int canUse = 0;
                int inUse = 0;
                int leave = 0;
                for (int j = 0; j < 90; j++)
                {
                    DataRow dr = roomdt.NewRow();
                    dr["currentday"] = DateTime.Now.AddDays(j).ToString("yyyy-MM-dd") + " " + GetWeekDayCh(DateTime.Now.AddDays(j).DayOfWeek);
                    dr["roomsum"]=roomlist.Count;
                    dr["canusesum"] = 0;
                    dr["inusesum"] = 0;
                    dr["departuresum"] = 0;
                  

                    //按房型统计
                    for (int n = 0; n < iCount; n++)
                    {

                            dr[dt.Rows[n]["Note"].ToString() + "roomsum"] = 0;
                            dr[dt.Rows[n]["Note"].ToString() + "canusesum"] = 0;
                            dr[dt.Rows[n]["Note"].ToString() + "inusesum"] =0;
                            dr[dt.Rows[n]["Note"].ToString() + "departuresum"] =0;

                    }
                    foreach (RoomInfo entity in roomlist)
                    {

                        canUse = 0;
                        inUse = 0;
                        leave = 0;
                        switch (entity.Status)
                        {
                            case Entity.RoomStatus.空净房:
                                canUse = 1;
                                break;
                            case Entity.RoomStatus.空脏房:
                                canUse = 1;
                                break;
                            case Entity.RoomStatus.住净房:
                            case Entity.RoomStatus.住脏房:
                                if (DateTime.Now.AddDays(j) >= entity.OrderInfo.CreateDate && DateTime.Now.AddDays(j) < entity.OrderInfo.PreOffDate)
                                {

                                 
                                    inUse = 1;

                                }
                                else
                                {
                                    canUse = 1;
                                    leave = 1;
                                }
                                break;
                            case Entity.RoomStatus.维修房: 
                                inUse = 0;
                                break;
                            case Entity.RoomStatus.预定房:
                                inUse = 1;
                                break;
                            case Entity.RoomStatus.锁房:
                                inUse = 0;
                                break;
                            case Entity.RoomStatus.强制锁房:
                                inUse = 0;
                                break;
                            case Entity.RoomStatus.钟点房:
                                inUse = 1;
                                leave = 1;
                                break;
                        }

                        //按房型统计
                        for (int k = 0; k < iCount; k++)
                        {

                            if (dt.Rows[k]["Note"].ToString() == entity.RoomTypeCode.ToString())
                            {

                                dr[dt.Rows[k]["Note"].ToString() + "roomsum"] = Convert.ToInt32(dr[dt.Rows[k]["Note"].ToString() + "roomsum"]) + 1;
                                dr[dt.Rows[k]["Note"].ToString() + "canusesum"] = Convert.ToInt32(dr[dt.Rows[k]["Note"].ToString() + "canusesum"]) + canUse;
                                dr[dt.Rows[k]["Note"].ToString() + "inusesum"] = Convert.ToInt32(dr[dt.Rows[k]["Note"].ToString() + "inusesum"]) + inUse;
                                dr[dt.Rows[k]["Note"].ToString() + "departuresum"] = Convert.ToInt32(dr[dt.Rows[k]["Note"].ToString() + "departuresum"]) + leave;


                            }
                        
                        }

              
               
                        //总的统计
                        dr["canusesum"] =Convert.ToInt32(dr["canusesum"]) +canUse;
                        dr["inusesum"] = Convert.ToInt32(dr["inusesum"]) + inUse;
                        dr["departuresum"] = Convert.ToInt32(dr["departuresum"]) + leave;
                        dr["inuseratesum"] = (Convert.ToDouble(dr["inusesum"]) / Convert.ToDouble(dr["roomsum"])).ToString("P2");
                        

                    }
                    roomdt.Rows.Add(dr);
                    this.dgView.ColumnHeadersHeight = 40;
                    this.dgView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                    this.dgView.DataSource = roomdt;
                
                
                
                }
           
           
           
           
           }

            
        
        
        }

        private string GetWeekDayCh(DayOfWeek dayofWeek)
        {
            string tempStr = "";
            switch (dayofWeek)
            { 
                case DayOfWeek.Monday:
                    tempStr= "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    tempStr = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    tempStr = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    tempStr = "星期四";
                    break;
                case DayOfWeek.Friday:
                    tempStr = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    tempStr = "星期六";
                    break;
                case DayOfWeek.Sunday:
                    tempStr = "星期日";
                    break;
            
            
            
            
            
            }

            return tempStr;
        
        
        
        }

        private void dgView_Scroll(object sender, ScrollEventArgs e)
        {
            dgView.ReDrawHead();
        }

        private void toolStripMenuBook_Click(object sender, EventArgs e)
        {


            FrmBookRoomNoFixNum dlg = new FrmBookRoomNoFixNum(); 
            dlg.ShowDialog();
            CreateReport();


        }

      

    }
}
